#include "webview.h"
#include <windows.h>
#include <WebView2.h>
#include <stdlib.h>
#include <stdio.h>

#pragma comment(lib, "WebView2Loader.dll.lib")

struct WebView {
    HWND hwnd;
    ICoreWebView2* webview;
    ICoreWebView2Controller* controller;
    WNDCLASSEXW wc;
};

static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    WebView* webview = (WebView*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
    
    switch (uMsg) {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

WebView* webview_create(WebViewConfig config) {
    WebView* webview = (WebView*)malloc(sizeof(WebView));
    if (!webview) return NULL;
    
    // 注册窗口类
    webview->wc.cbSize = sizeof(WNDCLASSEXW);
    webview->wc.lpfnWndProc = WindowProc;
    webview->wc.hInstance = GetModuleHandle(NULL);
    webview->wc.lpszClassName = L"WebView";
    
    RegisterClassExW(&webview->wc);
    
    // 创建窗口
    DWORD style = WS_OVERLAPPEDWINDOW;
    if (config.borderless) {
        style = WS_POPUP;
    }
    
    wchar_t title[256];
    MultiByteToWideChar(CP_UTF8, 0, config.title, -1, title, 256);
    
    webview->hwnd = CreateWindowExW(
        0,
        L"WebView",
        title,
        style,
        config.x,
        config.y,
        config.width,
        config.height,
        NULL,
        NULL,
        GetModuleHandle(NULL),
        NULL
    );
    
    SetWindowLongPtr(webview->hwnd, GWLP_USERDATA, (LONG_PTR)webview);
    
    // 创建 WebView2 环境
    CreateCoreWebView2EnvironmentWithOptions(NULL, NULL, NULL,
        &ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler {
            webview->controller->get_CoreWebView2(&webview->webview);
            
            // 隐藏滚动条
            if (config.hide_scrollbars) {
                // 注入 CSS 来隐藏滚动条
                const wchar_t* css = L"*::-webkit-scrollbar { display: none !important; }";
                webview->webview->AddScriptToExecuteOnDocumentCreated(css, NULL);
            }
        }
    );
    
    if (config.transparent) {
        COLORREF color = RGB(0, 0, 0);
        SetLayeredWindowAttributes(webview->hwnd, color, (BYTE)(255 * config.alpha), LWA_ALPHA);
    }
    
    return webview;
}

void webview_load_url(WebView* webview, const char* url) {
    if (webview && webview->webview) {
        wchar_t wurl[2048];
        MultiByteToWideChar(CP_UTF8, 0, url, -1, wurl, 2048);
        webview->webview->Navigate(wurl);
    }
}

void webview_load_html(WebView* webview, const char* html) {
    if (webview && webview->webview) {
        wchar_t whtml[8192];
        MultiByteToWideChar(CP_UTF8, 0, html, -1, whtml, 8192);
        webview->webview->NavigateToString(whtml);
    }
}

void webview_set_alpha(WebView* webview, float alpha) {
    if (webview && webview->hwnd) {
        SetLayeredWindowAttributes(webview->hwnd, 0, (BYTE)(255 * alpha), LWA_ALPHA);
    }
}

void webview_set_title(WebView* webview, const char* title) {
    if (webview && webview->hwnd) {
        wchar_t wtitle[256];
        MultiByteToWideChar(CP_UTF8, 0, title, -1, wtitle, 256);
        SetWindowTextW(webview->hwnd, wtitle);
    }
}

void webview_set_size(WebView* webview, int width, int height) {
    if (webview && webview->hwnd) {
        SetWindowPos(webview->hwnd, NULL, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER);
    }
}

void webview_set_position(WebView* webview, int x, int y) {
    if (webview && webview->hwnd) {
        SetWindowPos(webview->hwnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
    }
}

void webview_show(WebView* webview) {
    if (webview && webview->hwnd) {
        ShowWindow(webview->hwnd, SW_SHOW);
        UpdateWindow(webview->hwnd);
    }
}

void webview_hide(WebView* webview) {
    if (webview && webview->hwnd) {
        ShowWindow(webview->hwnd, SW_HIDE);
    }
}

void webview_destroy(WebView* webview) {
    if (webview) {
        if (webview->controller) {
            webview->controller->Release();
        }
        if (webview->webview) {
            webview->webview->Release();
        }
        if (webview->hwnd) {
            DestroyWindow(webview->hwnd);
        }
        free(webview);
    }
}

void webview_init(void) {
    // Windows 平台不需要特殊初始化
}

void webview_run(void) {
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
} 